package net.xxcxcxcx.xc_enhancedmob.skills.basics.individual.mystery;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.LightningEntity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.xxcxcxcx.xc_enhancedmob.config.skill.SkillConfig;
import net.xxcxcxcx.xc_enhancedmob.skills.BaseSkill;
import net.xxcxcxcx.xc_enhancedmob.skills.SkillRarity;

import java.util.Random;

public class ThunderSkill extends BaseSkill {

    public ThunderSkill(int level){super(level);}

    public ThunderSkill(NbtCompound nbtCompound){super(nbtCompound);}

    public ThunderSkill(){
    }

    @Override
    public SkillConfig getDefaultConfig() {
        SkillConfig skillConfig = super.getDefaultConfig();
        skillConfig.setMessage(getConfigMessage(skillConfig));
        return skillConfig;
    }

    @Override
    public String getConfigMessage(SkillConfig skillConfig) {
        if (skillConfig == null) return super.getConfigMessage(null);
        return "每隔一段时间向目标所在位置释放闪电";
    }

    public SkillRarity getSkillRarity() {
        return SkillRarity.Mystery;
    }

    @Override
    public boolean canUseAbility(MobEntity mobEntity) {
        return (mobEntity.world.getTime()>getSkilltime());
    }

    @Override
    public boolean onAbilityUse(MobEntity mobEntity) {
        LivingEntity target = mobEntity.getTarget();
        LightningEntity lightningEntity = EntityType.LIGHTNING_BOLT.create(mobEntity.world);
        if(target != null && canUseAbility(mobEntity))
        {
            Random random= new Random();
            int Randomnum1 = random.nextInt(3)-4;//释放次数偏移量，每次释放次数等于技能等级 - 释放次数偏移量
            int times = getSkillLevel()-Randomnum1;
            if (times<=0)
            {
                times = 1;
            }//随技能等级会连续触发多次,最少触发一次
            int i = 0;
            while(i<times)
            {
                if(canUseAbility(mobEntity)){
                    BlockPos blockPos = target.getBlockPos();
                    lightningEntity.refreshPositionAfterTeleport(Vec3d.ofBottomCenter(blockPos));
                    mobEntity.world.spawnEntity(lightningEntity);
                    int Randomnum2 = random.nextInt(10);
                    setSkilltime(getSkilltime()+getInsideCooldown()*(Randomnum2/10));//每次释放闪电的间隔
                    i++;
                }
            }
            setSkilltime(mobEntity.world.getTime()+getCooldown());
        }
        return true;
    }
}
